<template>
  <div class="app-container">
    <div class="filter-container" align="left">
      <el-input placeholder="请输入姓名搜索" style="width: 200px;" class="filter-item" v-model="search" />
      <el-button class="filter-item" type="primary" size="medium" icon="el-icon-search" @click="searchByName_">
        搜索
      </el-button>
      <!-- <el-select placeholder="输入类别搜索" clearable style="width: 200px" class="filter-item">
        <el-option v-for="item in importanceOptions" :key="item" :label="item" :value="item" />
        <el-option :key="item" :label="item" value="猫" />
        <el-option :key="item" :label="item" value="狗" />
      </el-select> -->
      <!-- <el-select placeholder="根据品种搜索" clearable style="width: 200px" class="filter-item">
        <el-option v-for="item in importanceOptions" :key="item" :label="item" :value="item" />
        <el-option :key="item" :label="item" value="金毛" />
        <el-option :key="item" :label="item" value="布偶" />
      </el-select> -->
    </div>
    <!-- 表格 -->
    <el-table :data="petList" style="width: 100%">
      <el-table-column label="序号" align="center" width="70" type="index" :index="ordernumber">
        <!-- <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="姓名" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.petName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center">
        <template slot-scope="scope">
          <img width="50" height="50" :src="scope.row.petImg" alt="">
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.petSex == 0 ? '男' : '女' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.petAge }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类别" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.petType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="品种" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.petBreed }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否免疫" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.petVaccine == 1 ? 'success' : 'danger'">{{ scope.row.petVaccine == 1 ? "已免疫" : "未免疫"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否绝育" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.petSterilization == 1 ? 'success' : 'danger'">{{ scope.row.petSterilization == 1 ?
            "已绝育" :
            "未绝育" }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否驱虫" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.petInisect == 1 ? 'success' : 'danger'">{{ scope.row.petInisect == 1 ? "已驱虫" : "未驱虫"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="描述" width="200" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.petDescr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button size="mini" plain type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" plain type="danger" @click="handleDelete(scope.row.petId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging" style="float: right;">
      <el-pagination @current-change="handleCurrentChange" :current-page.sync="pageNum" :page-size="10"
        layout="total, prev, pager, next" :total="total">
      </el-pagination>
    </div>
    <!-- 弹框 -->
    <el-dialog title="宠物信息" :visible.sync="dialogFormVisible" align="center" style="padding:10px">
      <el-form :model="form">
        <el-form-item label="宠物姓名" :label-width="formLabelWidth">
          <el-input v-model="form.petName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="宠物图片" :label-width="formLabelWidth">
          <el-input v-model="form.petImg" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="宠物年龄" :label-width="formLabelWidth" align="left">
          <el-select v-model="form.petAge" clearable placeholder="请选择年龄" align="left">
            <el-option v-for="item in ageOptions" :key="item.petageId" :label="item.petageType" :value="item.petageId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宠物类别" :label-width="formLabelWidth" align="left">
          <el-select v-model="form.petType" placeholder="请选择">
            <el-option v-for="item in typeOptions" :key="item.pettypeId" :label="item.pettypeName"
              :value="item.pettypeId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宠物品种" :label-width="formLabelWidth" align="left">
          <el-select v-model="form.petBreed" placeholder="请选择">
            <el-option v-for="item in breedOptions" :key="item.petbreedId" :label="item.petbreedName"
              :value="item.petbreedId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否免疫" :label-width="formLabelWidth" align="left">
          <el-radio v-model="form.petVaccine" label="0">未免疫</el-radio>
          <el-radio v-model="form.petVaccine" label="1">已免疫</el-radio>
        </el-form-item>
        <el-form-item label="是否绝育" :label-width="formLabelWidth" align="left">
          <el-radio v-model="form.petSterilization" label="0">未绝育</el-radio>
          <el-radio v-model="form.petSterilization" label="1">已绝育</el-radio>
        </el-form-item>
        <el-form-item label="是否驱虫" :label-width="formLabelWidth" align="left">
          <el-radio v-model="form.petInisect" label="0">未驱虫</el-radio>
          <el-radio v-model="form.petInisect" label="1">已驱虫</el-radio>
        </el-form-item>
        <el-form-item label="宠物描述" :label-width="formLabelWidth">
          <el-input v-model="form.petDescr" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus == 'update' ? updateData() : ''">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selectAll, update, del, searchByName, selectAllAge } from '../../api/petmanage/petinfo.js'
import { selectAll as selectAllpetType } from '../../api/petmanage/pettype.js'
import { selectAll as selectAllpetBreed } from '../../api/petmanage/petbreed.js'
export default {
  data() {
    return {
      value1: true,
      petList: [],
      form: {
      },
      updateId: null,
      dialogFormVisible: false,
      formLabelWidth: '100px',
      dialogStatus: '',
      search: '',
      ageOptions: [{ petageId: '', petageType: '' }],
      typeOptions: [{ pettypeId: '', pettypeName: '' }],
      breedOptions: [{ pettypeId: '', pettypeName: '' }],
      pageNum: 1,
      total: 10,
    }
  },
  computed: {
    ordernumber() {
      return (this.pageNum * 10 - 10) + 1;
    }
  },
  methods: {
   
    handleCurrentChange(val) {
      this.pageNum = val;
      this.selectAll_();
      console.log(`当前页: ${val}`);
    },
    handleUpdate(row) {
      this.form = Object.assign({}, row);
      this.form.petAge = row.petAgeId;
      this.form.petType = row.petTypeId;
      this.form.petBreed = row.petBreedId;
      this.dialogFormVisible = true;
      this.dialogStatus = 'update';
    },
    // 更新
    async updateData() {
      console.log(this.form)
      let res = await update(this.form);
      this.selectAll_();
      console.log(res)
      this.dialogFormVisible = false;
      this.$notify({
        title: 'Success',
        message: 'Update Successfully',
        type: 'success',
        duration: 2000
      })
    },
    // 删除
    async handleDelete(id) {
      let res = await del(id);
      this.$notify({
        title: 'Success',
        message: 'Delete Successfully',
        type: 'success',
        duration: 2000
      })
      this.selectAll_();
    },
    // 根据姓名查询
    async searchByName_() {
      let res = await searchByName(this.search, this.pageNum);
      console.log(res)
      this.petList = res.data.list
      this.total = res.data.total
    },
    // 查询所有
    async selectAll_() {
      let res = await selectAll(this.pageNum);
      console.log(res);
      this.petList = res.data.list
      this.total = res.data.total
    },
    // 查询所有宠物类型的信息
    async selectAllpetType_() {
      let res = await selectAllpetType();
      this.typeOptions = res.data
    },
    // 查询所有宠物类型的信息
    async selectAllAge_() {
      let res = await selectAllAge();
      // console.log(res)
      this.ageOptions = res.data;
    },
    // 查询所有宠物品种
    async selectAllpetBreed_() {
      let res = await selectAllpetBreed();
      // console.log(res)
      this.breedOptions = res.data
    },
  },
  created() {
    this.selectAll_();
    this.selectAllpetType_();
    this.selectAllAge_();
    this.selectAllpetBreed_();
  },
}
</script>
<style>
.paging {
  margin-top: 20px;
}
</style>